package edu.lingnan.rili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.lingnan.rili.controller.MarksController;
import edu.lingnan.rili.mapper.MatchMapper;
import edu.lingnan.rili.pojo.Addawarded;
import edu.lingnan.rili.pojo.Awarded;
import edu.lingnan.rili.pojo.Marks;
import edu.lingnan.rili.pojo.Match;
import edu.lingnan.rili.service.AddawardedService;
import edu.lingnan.rili.mapper.AddawardedMapper;
import edu.lingnan.rili.vo.AddAwardedVo;
import edu.lingnan.rili.vo.AwardedVo;
import edu.lingnan.rili.vo.PagingQueryResult;
import edu.lingnan.rili.vo.UploadingVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xbaozi
 * @date 2022/3/20 21:10
 */
@Service
public class AddawardedServiceImpl extends ServiceImpl<AddawardedMapper, Addawarded> implements AddawardedService {

    @Autowired
    private AddawardedMapper addawardedMapper;

    @Autowired
    private MatchMapper matchMapper;

    @Override
    public List<AddAwardedVo> selectByAnnounced() {
        //创建一个QueryWrapper的对象
        QueryWrapper<Addawarded> wrapper = new QueryWrapper<>();
        // 查询已公示的数据
        wrapper.eq("pubblicity_type", 1);
        // 按照时间排序

        List<Addawarded> addAwardedList = addawardedMapper.selectList(wrapper);
        ArrayList<AddAwardedVo> addAwardedVos = new ArrayList<>();

        if (!ObjectUtils.isEmpty(addAwardedList)) {
            for (Addawarded addawarded : addAwardedList) {
                AddAwardedVo addAwardedVo = new AddAwardedVo();
                BeanUtils.copyProperties(addawarded, addAwardedVo);
                Match match = matchMapper.selectById(addawarded.getUsersId());
                if (match != null) {
                    BeanUtils.copyProperties(match, addAwardedVo);
                    addAwardedVos.add(addAwardedVo);
                }
            }
            return addAwardedVos;
        }
        return null;
    }

    @Override
    public PagingQueryResult<AddAwardedVo> selectByAnnouncedByPage(Integer currentPage, Integer pageSize) {
        //创建一个QueryWrapper的对象
        QueryWrapper<Addawarded> wrapper = new QueryWrapper<>();
        // 查询已公示的数据
        wrapper.eq("pubblicity_type", 1);
        Page<Addawarded> addawardedPage = addawardedMapper.selectPage(new Page<Addawarded>(currentPage, pageSize), wrapper);

        // 新建分页统一格式对象
        PagingQueryResult<AddAwardedVo> pageResult = new PagingQueryResult<>();
        // 设置总数据条数
        pageResult.setPageTotal((int) addawardedPage.getTotal());
        // 设置当前显示数据条数
        pageResult.setRows(addawardedPage.getRecords().size());

        // 获取当前页的数据
        List<Addawarded> addawardedList = addawardedPage.getRecords();

        // 转换成Vo集合
        ArrayList<AddAwardedVo> addAwardedVos = new ArrayList<>();

        if (!ObjectUtils.isEmpty(addawardedList)) {
            for (Addawarded addawarded : addawardedList) {
                AddAwardedVo addAwardedVo = new AddAwardedVo();
                BeanUtils.copyProperties(addawarded, addAwardedVo);
                Match match = matchMapper.selectById(addawarded.getUsersId());
                if (match != null) {
                    BeanUtils.copyProperties(match, addAwardedVo);
                    addAwardedVos.add(addAwardedVo);
                }
            }
            pageResult.setDataList(addAwardedVos);
        }
        return pageResult;
    }

    @Override
    public Boolean addData(AddAwardedVo addAwardedVo) {
        // 判断学生信息是否错误
        Match match = matchMapper.selectById(addAwardedVo.getUsersId());
        if (match == null) {
            return false;
        }
        // 创建 AddAwarded 其他比赛加分对象，并赋予数据
        Addawarded addawarded = new Addawarded();
        BeanUtils.copyProperties(addAwardedVo, addawarded);
        addawarded.setAddawardedTime(LocalDateTime.now());
        // 设置未公示
        addawarded.setPubblicityType(Marks.NOT_ANNOUNCED);
        System.out.println(addawarded);
        int row = addawardedMapper.insert(addawarded);
        return row > 0;
    }
}




